package com.sinodata.bsm.cicp.finder.jboss;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.management.ObjectName;

import org.apache.log4j.Logger;
import org.jboss.jmx.adaptor.rmi.RMIAdaptor;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.RmiUtil;

/**
 * 对Jboss 线程池的发现
 */
public class ThreadpoolFinder extends Finder {

    private static final Logger logger = Logger.getLogger(ThreadpoolFinder.class);

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        String port = (String) params.get("PORT");
        List<String[]> list = new ArrayList<String[]>();
        try {
            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(port));
            ObjectName jbossThreadPool = new ObjectName("jboss.system:service=ThreadPool");
            String managerName = (String) rmiAdapter.getAttribute(jbossThreadPool, "Name");
            String[] info = new String[4];
            list.add(info);
            info[0] = ip + "_JbossThreadPool_" + managerName;
            info[1] = "JBoss Thread Pool";
            info[2] = ip + "_JbossThreadPool_" + managerName;
            info[3] = managerName;
            return list;
        } catch (Exception e) {
            logger.error("failed to find jboss ManagedConnectionPool ", e);
        }
        return null;
    }
}
